﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Factory.Framework;
namespace ExampleWebAppUsage
{
    public partial class Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            
        }

        #region Email Factory Using Example

        protected void btnSendEmail_Click(object sender, EventArgs e)
        {
            // create key value dictionary for parse and replace placeholders in email template
            var templateKeyValues = new Dictionary<string, string>();

            //this is example for existing placeholders in EmailTemplates/test.template
            templateKeyValues.Add("{%FIRST_NAME%}", "Jack");
            templateKeyValues.Add("{%LAST_NAME%}", "Sparrow");

            string emailFactoryConfigPath = "~/App_Data/EmailFactory/EmailFactory.config";
            EmailFactory email = new EmailFactory(emailFactoryConfigPath); // this will fire with the default mode which is setup in Email.Config

            // EmailFactory email = new EmailFactory("production"); // We can also set mode manualy 

            // EmailFactory email = new EmailFactory("test");        

            bool sendingStatus = false;
            try
            {
                // you can send mail to multiple addresses

                // email.SendMail(new string[] { "username@domain.com","username2@somedomain.com" }, "test.template", templateKeyValues);            

                string[] toEmails = new string[] { tbxSendToEmail.Text };
                string[] attachedFilesPath = null;
                if (FileUpload1.FileName != string.Empty)
                {
                    attachedFilesPath = new string[] { this.UploadFiles() };
                }

                sendingStatus = email.SendMail(toEmails, "test.template", templateKeyValues, attachedFilesPath);
            }
            catch (Exception ex)
            {
                //
            }

            if (sendingStatus)
                lblStatus.Text = "Mail sending successfull";
            else
                lblStatus.Text = "Mail was not sent, becouse of email mode is disabled";


        }

        private string UploadFiles()
        {
            Random rnd = new Random();
            string rndChars = Guid.NewGuid().ToString().Substring(0, 8);

            string filePath = Server.MapPath("~/App_Data/EmailFactory/EmailAttachments/");
            string fileName = string.Format("{0}_{1}_{2}", DateTime.Now.ToString("yyyyMMddHHmmss"), rndChars ,FileUpload1.FileName);
            string fullPath = filePath + fileName;

            FileUpload1.SaveAs(fullPath);

            return fullPath;
        }

        #endregion

        #region Event Factory Using Example

        protected void btnAddEvent_Click(object sender, EventArgs e)
        {

            EventType eventType = EventType.Information;// default 

            switch (rbxErrorTypes.SelectedValue)
            {
                case "Information":
                    eventType = EventType.Information;
                    break;
                case "Warning":
                    eventType = EventType.Warning;
                    break;

                case "Error":
                    eventType = EventType.Error;
                    break;

            }

            string eventFactoryConfigPath = "~/App_Data/EventFactory/EventFactory.config";
            string emailFactoryConfigPath = "~/App_Data/EmailFactory/EmailFactory.config";
            EventFactory newEvent = new EventFactory(eventFactoryConfigPath,emailFactoryConfigPath, eventType, tbxEventDescription.Text, EventNotificationType.Save);

            //// EventFactory newEvent = new EventFactory(eventFactoryConfigPath,emailFactoryConfigPath, eventType, tbxEventDescription.Text, EventNotificationType.Save);
            //// EventFactory newEvent = new EventFactory(eventFactoryConfigPath,emailFactoryConfigPath, eventType, tbxEventDescription.Text, EventNotificationType.Email);
            //// EventFactory newEvent = new EventFactory(eventFactoryConfigPath,emailFactoryConfigPath, eventType, tbxEventDescription.Text, EventNotificationType.EmailAndSave);
            //// EventFactory newEvent = new EventFactory(eventFactoryConfigPath,emailFactoryConfigPath, eventType, EventColor.Red, " Some Description", EventNotificationType.Save);
            //// EventFactory someEvent = new EventFactory(eventFactoryConfigPath,emailFactoryConfigPath);
            //// someEvent.AddEvent(eventType, tbxEventDescription.Text, EventNotificationType.EmailAndSave);

            Response.Redirect(Request.Url.ToString());
        }

        #endregion

    }
}
